// 渲染
function apply() {
    $('tbody').html('')
    $.ajax({
        type: 'get',
        url: `https://liu.zzgoodqc.cn/goodsx/listByPage?page=1&limit=4`,
        dataType: 'json',
        success: (a) => {
            a.data.forEach(item => {
                $('tbody').append(`
                    <tr>
                    <td>${item.id}</td>
                    <td>${item.goods_name}</td>
                    <td>${item.stock}</td>
                    <td>${item.price}</td>
                    <td>${item.status == 1 ? '上架' : item.status == 2 ? "下架" : "售罄"}</td>
                    <td>${item.sold}</td>
                    <td><img src ="${item.img}"></td>
                    <td class="cz">
                    <button onclick="del(${item.id})">删除</button>
                    <button onclick="gai(${item.id})">更新</button>
                    <button onclick="xiang(${item.id})">详情</button>
                    <button onclick="kes(${item.id},${item.status})" class="gg">更新状态</button>
                    </td>
                    </tr>`)
            })
        },
        error: (e) => {
            console.log(e);
        }
    })
}
apply()

// 添加
$('.add').hide()
function jia() {
    $('.add').show()
}

function add() {
    $.ajax({
        type: 'post',
        url: 'https://liu.zzgoodqc.cn/goodsx/add',
        data: {
            goods_name: $('.add input:eq(0)').val(),
            stock: $('.add input:eq(1)').val(),
            price: $('.add input:eq(2)').val(),
            status: $('.add input:eq(3)').val(),
            sold: $('.add input:eq(4)').val(),
            img: $('.add input:eq(5)').val()
        },
        dataType: 'json',
        success: (o) => {
            $('tbody').html('')
            apply()
            $('.add').hide()
        },
        error: (e) => {
            console.log(e);
        }
    })
}

// 删除
function del(id) {
    $.ajax({
        type: 'get',
        url: 'https://liu.zzgoodqc.cn/goodsx/delete?id=' + id,
        data: {},
        dataType: 'json',
        success: () => {
            $('tbody').html('')
            apply()
        },
        error: (e) => {
            console.log(e);
        }
    })
}

//修改
var iid
$(".xiu").hide()
function gai(id) {
    iid = id
    $(".xiu").show()
}
function xiugai() {
    $.ajax({
        type: "post",
        url: "https://liu.zzgoodqc.cn/goodsx/update?id=" + iid,
        data: {
            goods_name: $(".xiu input").eq(0).val(),
            stock: $(".xiu input").eq(1).val(),
            price: $(".xiu input").eq(2).val(),
            status: $(".xiu input").eq(3).val(),
            sold: $(".xiu input").eq(4).val(),
            img: $(".xiu input").eq(5).val(),
        },
        dataType: "json",
        success: () => {
            $('tbody').html('')
            apply()
            $(".xiu").hide()
        },
        error: (e) => {
            console.log(e);
        }
    })
}

//查找
function zhao() {
    var jk = $(".cha").val()
    $.ajax({
        type: "post",
        url: "https://liu.zzgoodqc.cn/goodsx/search?goods_name=" + jk,
        data: {},
        dataType: "json",
        success: (b) => {
            var bd = (b.data);
            console.log(bd);
            bd.forEach(item => {
                $("tbody").html(`
                <tr>
                   <td>${item.id}</td>
                   <td>${item.goods_name}</td>
                   <td>${item.stock}</td>
                   <td>${item.price}</td>
                   <td>${item.status == 1 ? '上架' : item.status == 2 ? "下架" : "售罄"}</td>
                   <td>${item.sold}</td>
                   <td><img src="${item.img}"></td>
                   <td class="cz">
                        <button onclick="del(${item.id})">删除</button>
                        <button onclick="gai(${item.id})">修改</button>
                        <button onclick="xiang(${item.id})">详情</button>
                        <button onclick="kes(${item.id},${item.status})" class="gg">更新状态</button>
                    </td>
                </tr>
                   `)
            })
        },
        error: (e) => {
            console.log(e);
        }
    })
}

// 排序
function p(a1, b2) {
    $.ajax({
        type: 'get',
        url: `https://liu.zzgoodqc.cn/goodsx/sort?sort=${a1}&type=${b2}&page=1&limit=4`,
        data: {},
        dataType: 'json',
        success: (a) => {
            $('tbody').html('')
            a.data.forEach(item => {
                $('tbody').append(`
                <tr>
                   <td>${item.id}</td>
                   <td>${item.goods_name}</td>
                   <td>${item.stock}</td>
                   <td>${item.price}</td>
                   <td>${item.status == 1 ? '上架' : item.status == 2 ? "下架" : "售罄"}</td>
                   <td>${item.sold}</td>
                   <td><img src="${item.img}"></td>
                   <td class="cz">
                        <button onclick="del(${item.id})">删除</button>
                        <button onclick="gai(${item.id})">修改</button>
                        <button onclick="xiang(${item.id})">详情</button>
                        <button onclick="kes(${item.id},${item.status})" class="gg">更新状态</button>
                        </td>
                </tr>
                `)
            })
        },
        error: (e) => {
            console.log(e);
        }
    })
}
p("asc", "price")

// 排序判断
function sort() {
    var s1 = $('select').eq(0).val()
    var s2 = $('select').eq(1).val()
    if (s1 == '价格' && s2 == '从大到小') {
        p('desc', 'price')
    } else if (s1 == '价格' && s2 == '从小到大') {
        p('asc', 'price')
    }
    if (s1 == '库存' && s2 == '从大到小') {
        p('desc', 'stock')
    } else if (s1 == '库存' && s2 == '从小到大') {
        p('asc', 'stock')
    }
    if (s1 == '已售' && s2 == '从大到小') {
        p('desc', 'sold')
    } else if (s1 == '已售' && s2 == '从小到大') {
        p('asc', 'sold')
    }

}

// 更新状态
function kes(id, xin) {
    if (xin == 1 && "上架") {
        xin = 2
    } else if (xin == 2 && "下架") {
        xin = 1
    }
    $.ajax({
        type: 'get',
        url: `https://liu.zzgoodqc.cn/goodsx/updateStatus?id=${id}&status=${xin}`,
        data: {},
        dataType: 'json',
        success: (m) => {
            $('tbody').html('')
            apply()
        },
        error: (e) => {
            console.log(e);
        }
    })
}

// 分页
function fy1() {
    $.ajax({
        type: 'get',
        url: `https://liu.zzgoodqc.cn/goodsx/listByPage?page=1&limit=4`,
        data: {},
        dataType: 'json',
        success: (d) => {
            $('tbody').html('')
            d.data.forEach(item => {
                $('tbody').append(`
                    <tr>
                        <td>${item.id}</td>
                        <td>${item.goods_name}</td>
                        <td>${item.stock}</td>
                        <td>${item.price}</td>
                        <td>${item.status == 1 ? '上架' : item.status == 2 ? "下架" : "其他"}</td>    
                        <td>${item.sold}</td>
                        <td><img src="${item.img}"></td>
                        <td class="cz">
                            <button onclick="del(${item.id})">删除</button>
                            <button onclick="gai(${item.id})">修改</button>
                            <button onclick="xiang(${item.id})">详情</button>
                            <button onclick="kes(${item.id},${item.status})" class="gg">更新状态</button>
                        </td>
                    </tr>`)
            })
        },
        error: (e) => {
            console.log(e);
        }
    })
}

function fy2() {
    $.ajax({
        type: 'get',
        url: `https://liu.zzgoodqc.cn/goodsx/listByPage?page=2&limit=4`,
        data: {},
        dataType: 'json',
        success: (d) => {
            $('tbody').html('')
            d.data.forEach(item => {
                $('tbody').append(`
                    <tr>
                        <td>${item.id}</td>
                        <td>${item.goods_name}</td>
                        <td>${item.stock}</td>
                        <td>${item.price}</td>
                        <td>${item.status == 1 ? '上架' : item.status == 2 ? "下架" : "其他"}</td>    
                        <td>${item.sold}</td>
                        <td><img src="${item.img}"></td>
                        <td class="cz">
                            <button onclick="del(${item.id})">删除</button>
                            <button onclick="gai(${item.id})">修改</button>
                            <button onclick="xiang(${item.id})">详情</button>
                            <button onclick="kes(${item.id},${item.status})" class="gg">更新状态</button>
                        </td>
                    </tr>`)
            })
        },
        error: (e) => {
            console.log(e);
        }
    })
}

// 详情
function xiang(id) {
    location.assign('../详情.html?id=' + id)
}

// 个人主页
function g() {
    location.assign("../个人中心.html")
}

function zz(){
    location.assign("../柱状图.html")
}

function bz(){
    location.assign("../饼状图.html")
}

function dz(){
    location.assign("../点状图.html")
}

function zx(){
    location.assign("../折线图.html")
}